#include <interface\frame.h>
#include <interface\gl.h>

frame::frame(): fieldSet(),
                ICX( 0 ),
                ICY( 0 ),
                dragEnabled( true ),
                closed(false),
                minimized(false),
                locked(false),
                bar( NULL )
{
    ELname = "<frame>";
}
frame::frame(window* win):  fieldSet( win ),
                            ICX( 0 ),
                            ICY( 0 ),
                            dragEnabled( true ),
                            closed(false),
                            minimized(false),
                            locked(false),
                            bar( NULL )
{
    ELname = "<frame>";
}
frame::~frame(){}

void frame::setBar(winBar* mybar){
    bar = mybar;
    if( bar ) bar->setParentSet(this);
}

void frame::setDrag(bool drg){
    dragEnabled = drg;
}
bool frame::getDrag(){
    return dragEnabled;
}

void frame::setClosed(bool cl){
    closed = cl;
}
bool frame::getClosed(){
    return closed;
}

void frame::setMinimized(bool min){
    minimized = min;
}
bool frame::getMinimized(){
    return minimized;
}

void frame::setLocked(bool lk){
    locked = lk;
    if( bar ) bar->setDrag(!lk);
}
bool frame::getLocked(){
    return locked;
}

void frame::display(){
    if( closed ) return;
    if(bar){
        bar->display();
        bar->display_end();
    }
    if( minimized ) return;
    if( !styleS ) return;
    winElmt::display();
    glDisable(GL_SCISSOR_TEST);
    glColor(bkgColFromSS(elm_frame));
    int Xoff = 0;
    int Yoff = 0;
    getOffset(&Xoff,&Yoff);
    glDrawRect(enc,Xoff,Yoff);
    elDraw();
}

unsigned char frame::eventExec(unsigned char in){
    if( closed ) return in;
    if( !parentWin ) return in;
    if( !minimized && !locked ) in = elEventExec(in);
    unsigned char oldstatus = in;
    if(bar){
        in = bar->eventExec(in);
        if( (in & ACTIVE_TAKEN) && !(in & NOT_ACTIVABLE ) && !(oldstatus & ACTIVE_TAKEN) ){
            if( !locked ) activeGetEvent();
        }else{
            if( !minimized ) in = winElmt::eventExec(in);
        }
    }else{
        if( !minimized ) in = winElmt::eventExec(in);
    }
    if((in & ACTIVE_TAKEN)&&!(oldstatus & ACTIVE_TAKEN)){
        in = elEventExec(in);
    }
    return in;
}

void frame::activeGetEvent(){
    winElmt::activeGetEvent();
    if( bar ) bar->activeGetEvent();
}
void frame::activeLoseEvent(){
    winElmt::activeLoseEvent();
    if( bar ) bar->activeLoseEvent();
}

void frame::draggingEvent(int x,int y){
    winElmt::draggingEvent(x,y);
    if( bar ) return;
    if( !dragEnabled ) return;
    move(x,y);
}
